﻿Module Fastweb
    Dim TabPErmutazioni() As String = _
  Split("6216;2126;5343;5433;3531;3642;1512;2521;" & _
        "3533;4245;5254;6266;3216;2122;5324;4463;" & _
        "5565;6251;3616;3246;6335;3425;1554;4164;" & _
        "5411;4322;3236;2454;1345;1133;1116;2225;" & _
        "5133;4441;1352;6661;1561;2263;3362;4434;" & _
        "2135;2636;1251;2225;3333;4444;6512;5166;" & _
        "2161;1262;3353;4554;5426;6625;4126;4215;" & _
        "5336;5442;3541;3462;4234;6123;6452;1341", ";")

    Public Function GetWpa_Telsey(ByVal mac As String) As String
        Dim wpa As String = ""

        Dim s1 As UInteger = 0
        Dim TableInput() As UInteger = MacParmutation(mac, TabPErmutazioni)
        For n As Integer = 0 To TabPErmutazioni.Length - 1
            s1 = lookup3(TableInput, 0, n, s1)
        Next

        Dim s2 As UInteger = 0
        Dim TableInput2() As UInteger = MacParmutationBitwise(MacParmutation(mac, TabPErmutazioni))
        For n As Integer = 0 To TabPErmutazioni.Length - 1
            s2 = lookup3(TableInput2, 0, n, s2)
        Next

        wpa = s1.ToString("X8").Substring(3, 5) & s2.ToString("X8").Substring(0, 5)
        Return LCase(wpa)

    End Function

    Private Function MacParmutation(ByVal macAddress As String, ByVal Table() As String) As UInt32()
        Dim mac(0 To 6) As String
        For i As Short = 0 To 6
            mac(i) = Mid("XX" & macAddress, (i * 2) + 1, 2)
        Next
        'Dim mac() As String = Split("XX:" & macAddress, ":")
        Dim MAC_Permutato(63) As UInt32

        For n As Integer = 0 To 63
            MAC_Permutato(n) = "&H" & mac(Table(n).Substring(0, 1)) & _
            mac(Table(n).Substring(1, 1)) & mac(Table(n).Substring(2, 1)) & _
            mac(Table(n).Substring(3, 1))
        Next

        Return MAC_Permutato

    End Function

    Private Function MacParmutationBitwise(ByVal Table() As UInt32) As UInt32()
        Dim Mac_Permutato() As UInt32 = Table

        For n As Integer = 0 To Mac_Permutato.Length - 1

            Select Case n
                Case Is < 8
                    Mac_Permutato(n) = Mac_Permutato(n) << 3
                Case Is < 16
                    Mac_Permutato(n) = Mac_Permutato(n) >> 5
                Case Is < 32
                    Mac_Permutato(n) = Mac_Permutato(n) >> 2
                Case Is >= 32
                    Mac_Permutato(n) = Mac_Permutato(n) << 7
            End Select

        Next

        Return Mac_Permutato
    End Function

    Private Function lookup3(ByVal k() As UInt32, ByVal offset As Integer, ByVal length As Integer, ByVal initval As UInt32) As UInt32
        Dim a, b, c As UInt32
        c = &HDEADBEEFUI + (length << 2) + initval
        b = c
        a = b

        Dim i As Integer = offset

        Do While length > 3

            a += k(i + 0)
            b += k(i + 1)
            c += k(i + 2)

            '// FUNZIONE MIX (A,B,C)

            a -= c
            a = a Xor ((c << 4) Or (c >> 32 - 4))
            c += b
            b -= a
            b = b Xor ((a << 6) Or (a >> 32 - 6))
            a += c
            c -= b
            c = c Xor ((b << 8) Or (b >> 32 - 8))
            b += a
            a -= c
            a = a Xor ((c << 16) Or (c >> 32 - 16))
            c += b
            b -= a
            b = b Xor ((a << 19) Or (a >> 32 - 19))
            a += c
            c -= b
            c = c Xor ((b << 4) Or (b >> 32 - 4))
            b += a

            i = i + 3
            length -= 3

        Loop

        Select Case length
            Case 3
                c += k(i + 2)
                b += k(i + 1)
                a += k(i + 0)
            Case 2
                b += k(i + 1)
                a += k(i + 0)
            Case 1
                a += k(i + 0)
            Case 0
                GoTo fine
        End Select

        '// FUNZIONE FINALE (A,B,C)

        c = c Xor b
        c -= (b << 14) Or (b >> 32 - 14)
        a = a Xor c
        a -= (c << 11) Or (c >> 32 - 11)
        b = b Xor a
        b -= (a << 25) Or (a >> 32 - 25)
        c = c Xor b
        c -= (b << 16) Or (b >> 32 - 16)
        a = a Xor c
        a -= (c << 4) Or (c >> 32 - 4)
        b = b Xor a
        b -= (a << 14) Or (a >> 32 - 14)
        c = c Xor b
        c -= (b << 24) Or (b >> 32 - 24)

fine:

        Return c

    End Function

    Public Function GetWpa_Pirelli(ByVal SSID As String) As String
        Dim MD5_encoding As New System.Security.Cryptography.MD5CryptoServiceProvider
        SSID &= "223311340281FA22114168111201052271421066"
        Dim rtn_byte() As Byte = MD5_encoding.ComputeHash(HexStringToByte(SSID))
        Dim Str_MD5 As String = ""
        For Each b As Byte In rtn_byte
            Str_MD5 &= b.ToString("x2")
        Next
        Dim binary_str As String = ""
        For i As Byte = 0 To 3
            binary_str &= System.Convert.ToString(rtn_byte(i), 2).PadLeft(8, "0")
            ' binary_str &= rtn_byte(i).ToString("x").PadLeft(8, "0")
        Next
        Dim fast_wpa As String = "", fast_wpa_part As Byte
        For i As Integer = 0 To 24 Step 5
            fast_wpa_part = System.Convert.ToInt16(binary_str.Substring(i, 5), 2)
            If fast_wpa_part > 10 Then
                fast_wpa_part += 87
                fast_wpa &= fast_wpa_part.ToString("x2").PadLeft(2, "0")
            Else
                fast_wpa &= fast_wpa_part.ToString("x2").PadLeft(2, "0")
            End If
        Next
        Return fast_wpa
    End Function
    Public Function detailed_fast_WPA(ByVal fast_ssid As String)
        Dim detailedWPA As String = ""
        Dim a As String = fast_ssid.Substring(10, 6)
        Select Case UCase(fast_ssid.Substring(10, 6))
            Case "00036F", "002196"
                Dim wpa_fast_telsey = GetWpa_Telsey(fast_ssid.Substring(10, 12))
                detailedWPA = "TELSEY : " & UCase(fast_ssid.Substring(10, 12)) & "   " & "WPA: " & wpa_fast_telsey
            Case "6487D7", "38229D", "00A02F", "002553", "00238E", "002233", "001D8B", "001CA2", "00193E", "0017C2", "0013C8", "000827"
                Dim wpa_fast_pirel = GetWpa_Pirelli(fast_ssid.Substring(10, 12))
                detailedWPA = "PIRELLI: " & UCase(fast_ssid.Substring(10, 12)) & "   " & "WPA: " & wpa_fast_pirel
            Case Else
                MessageBox.Show("l SSID inserito non appartiene a pirelli o telsey")
        End Select
        Return detailedWPA
    End Function
End Module
